package com.wondertek.mist.plugins;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.eternity.classes.ATRP.Callback;
import com.innofidei.tools.InnofideiTools;
import com.returntoappstore.ICpicAppstoreService;
import com.wondertek.mist.Utils;

public class MAMPlugin extends CordovaPlugin{

	
	private Object lock=new Object();
	private ICpicAppstoreService mCpicAppstoreService;
	private ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mCpicAppstoreService = null;
		}

		@Override
		public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
			mCpicAppstoreService = ICpicAppstoreService.Stub.asInterface(iBinder);
			Log.i("MistActivity","onServiceConnected");
			synchronized (lock) {
				lock.notifyAll();
			}
		}
	};

	private String buildEncryptCode() {
		String encryptCode = Utils.getString(this.cordova.getActivity(), "mam.encryptCode");
		if (encryptCode != null && !encryptCode.equals("")) {
			try {
				encryptCode = InnofideiTools.encrypt(encryptCode,"CpicAppstore");
			} catch (Exception e) {
				e.printStackTrace();
			}
			return encryptCode;
		}
		return "";
	}
	
	
	@Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		final Activity context=this.cordova.getActivity();
		if("returnToAppstore".equalsIgnoreCase(action)){
			new Thread(){

				@Override
				public void run() {
					String aidl=Utils.getMetaValue(MAMPlugin.this.cordova.getActivity(), "mam.appstoreAidl");
					Intent intent = new Intent(aidl);
					Log.i("MistActivity",aidl);
					context.bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
					while(mCpicAppstoreService==null){
						synchronized (lock) {
							try {
								lock.wait(1000);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					Log.i("MistActivity","bindFinish");
					try {
						mCpicAppstoreService.returnAppstore(buildEncryptCode());
					} catch (RemoteException e) {
						e.printStackTrace();
					}finally{
						Log.i("MAMPlugin", "exit");
						//退出
						System.exit(0);
						Callback cb=new Callback() {
		                    
		        			public void onStart() {        				
		        			}

		        			public void onFailure(String msg) {  
		        				System.exit(0);
		        			}

		        			@Override
		        			public void onSuccess(JSONObject result, Header[] headers) { 
		        				System.exit(0);
		        			}
		        		
		        		};
						Utils.sendMamLog(MAMPlugin.this.cordova.getActivity(), "quit",cb);
						
					}

				}
			}.start();
			//callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
		}
		return true;
	}

}
